##REPLICATION FILE

##EXIT POLL DATA
library(ggplot2)
library(gridExtra)
library(gplots)
library(stargazer)

exit = read.csv("Exit Poll Replication Data.csv")


##TABLES 1 AND 2
t1 = table(exit$precinct, exit$race)
t2 = table(exit$precinct, exit$registration)

#TABLE 1: RACIAL DEMOGRAPHY AND INCOME STATISTICS BY PRECINCT
prop.table(t1, margin = 1)
aggregate(exit$Income~exit$precinct, FUN=mean)


#TABLE 2: PARTY REGISTRATION STATISTICS BY PRECINCT
prop.table(t2, margin = 1)


##GROUP MEANS FOR TABLE 3 (TOP HALF)
tapply(exit$MenBetterPol, exit[c("group")], mean, na.rm=TRUE)
tapply(exit$ControlWife, exit[c("group")], mean, na.rm=TRUE)


##RACIAL GROUP SUBSETS OF THE DATA
whites=subset(exit, exit$race=="white")
blacks=subset(exit, exit$race=="black")
latinos=subset(exit, exit$race=="latino")
others=subset(exit, exit$race == "other" | exit$race == "mixed")

##SIGNIFICANCE TESTS FOR TABLE 3 (TOP HALF)
t.test(whites$MenBetterPol ~ whites$group)
t.test(whites$ControlWife ~ whites$group)

t.test(blacks$MenBetterPol ~ blacks$group)
t.test(blacks$ControlWife ~ blacks$group)

t.test(latinos$MenBetterPol ~ latinos$group)
t.test(latinos$ControlWife ~ latinos$group)

t.test(others$MenBetterPol ~ others$group)
t.test(others$ControlWife ~ others$group)


#REGRESSION MODELS

#TABLE 4: Sexism and Candidate Support, Exit Poll Data
vote=lm(votetrump ~ MenBetterPol + ControlWife + race + Woman + registration + ReligAttend + BibLit + nativity + Regulation + lessgovt + ideology + trustmedia + votefraud + NoSpecialTreat + precinct+ DecreaseIllegalImm + Age +Income , data=exit)
summary(vote) 


favor.clinton=lm(Clinton.opinion ~ MenBetterPol + ControlWife + race + Woman + registration + ReligAttend + BibLit + nativity + Regulation + lessgovt + ideology + trustmedia + votefraud + NoSpecialTreat + precinct+ DecreaseIllegalImm +Age + Income, data=exit)
summary(favor.clinton)

favor.trump=lm(Trump.opinion ~ MenBetterPol + ControlWife + race + Woman + registration + ReligAttend + BibLit + nativity + Regulation + lessgovt + ideology + trustmedia + votefraud + NoSpecialTreat + precinct+ DecreaseIllegalImm + Age + Income , data=exit)
summary(favor.trump) 


#OUTPUT TABLE 4
stargazer(vote, favor.clinton, favor.trump, title="Sexism and Candidate Support ", align=TRUE, style="apsr", digits=2, column.labels=c("Vote", "Favor Clinton", "Favor Trump"), single.row = TRUE,  star.char=c("*", "**"), star.cutoffs=c(0.10, 0.05))


#MAKE NEW RACIAL GROUP SUBSET FOR NON-WHITES
notwhites=subset(exit, exit$race != "white")


##TABLE 5: Sexism and Trump Vote by Race, Exit Poll Data

vote.whites=lm(votetrump ~ MenBetterPol + ControlWife  + Woman + registration + ReligAttend + BibLit + nativity + Regulation + lessgovt + ideology + trustmedia + votefraud + NoSpecialTreat + precinct + DecreaseIllegalImm +Age +Income, data=whites)
summary(vote.whites) 

vote.whites.interact=lm(votetrump ~ MenBetterPol * Woman + ControlWife  + registration + ReligAttend + BibLit + nativity + Regulation + lessgovt + ideology + trustmedia + votefraud + NoSpecialTreat + precinct + DecreaseIllegalImm +Age + Income, data=whites)
summary(vote.whites.interact) 

##non white respondents

vote.nonwhites =lm(votetrump ~ MenBetterPol + ControlWife  + race + Woman + registration + ReligAttend + BibLit + nativity + Regulation + lessgovt + ideology + trustmedia + votefraud + NoSpecialTreat + precinct + DecreaseIllegalImm + Age + Income , data=notwhites)
summary(vote.nonwhites) 


#OUTPUT TABLE 5
stargazer(vote.whites, vote.whites.interact, vote.nonwhites, title="Sexism and Trump Vote, Exit Poll Data", align=TRUE, style="apsr", digits=2, column.labels=c("White Rs", "White Rs", "Non-White Rs"), single.row = TRUE,  star.char=c("*", "**"), star.cutoffs=c(0.10, 0.05))


#FIGURE 1: Gender Attitudes and Predicted Vote Choice for White Men and Women, Exit Poll Data

##From interaction model of White respondents in Table 5

MenBetterPol = c(0, 0, 1, 1, 2, 2, 3, 3)
ControlWife = mean(whites$ControlWife, na.rm = TRUE)
Woman = c(0, 1)
registration = "Democrat" 
ReligAttend = mean(whites$ReligAttend, na.rm = TRUE)
BibLit = mean(whites$BibLit, na.rm = TRUE)
nativity = 1
Regulation = mean(whites$Regulation, na.rm = TRUE)
lessgovt = 1
ideology = mean(whites$ideology, na.rm = TRUE)
trustmedia = mean(whites$trustmedia, na.rm = TRUE)
votefraud = mean(whites$votefraud, na.rm = TRUE)
NoSpecialTreat = mean(whites$NoSpecialTreat, na.rm = TRUE)
precinct = "WhitePre"
DecreaseIllegalImm = mean(whites$DecreaseIllegalImm, na.rm = TRUE)
Age = mean(whites$Age, na.rm = TRUE)
Income = mean(whites$Income, na.rm = TRUE)
grouping = c("White Men", "White Women")


ND = data.frame(MenBetterPol, ControlWife, Woman, registration, ReligAttend, BibLit, nativity, Regulation, lessgovt, ideology, trustmedia, votefraud, NoSpecialTreat, precinct, DecreaseIllegalImm, Age, Income, grouping)

pred.vote=predict(vote.whites.interact, newdata=ND, type="response",  interval="confidence", level=0.95)
ND$estimate = pred.vote[ , 1]
ND$lwr = pred.vote[ , 2]
ND$upr = pred.vote[ , 3]

##white women: men better suited associated with 35 percentage point increase in trump vote
##white men: men better suited associated with 9 percentage point increase in trump vote

#OUTPUT FIGURE 1
pred.plot = ggplot(ND, aes(MenBetterPol, estimate, ymin = 0, ymax = .6,)) + 
  
  ##plot estimates
  geom_point(aes(colour = grouping)) + 
  
  ##plot 95% confidence intervals
  geom_segment(aes(colour = factor(grouping), 
                   x = MenBetterPol, xend=MenBetterPol, 
                   y = lwr, yend = upr)) +

  geom_line(aes(x = MenBetterPol, y = estimate, group = grouping, colour = grouping, linetype = grouping), size=.8)  +
  
  scale_linetype_manual(values=c("dashed", "solid")) +
  scale_color_manual(values=c( "black", "dark gray"))+
  #facet_wrap(~treat, ncol=1) +
  theme_bw(  )  +
  theme(legend.title=element_blank())+
  theme(legend.position=c(.2, .85))+
  theme(legend.text = element_text(size=16))+
  theme(axis.text=element_text(size=16))+ 
  theme(plot.title = element_text(size =16)) +
  theme(axis.title.x = element_text(size = 16)) +
  theme(axis.title.y = element_text(size = 16)) +
xlab("Men Better Suited Emotionally for Politics") +  ylab("Trump Vote") +
  ggtitle("") 


##SURVEY SAMPLING INTERNATIONAL DATA
natl = read.csv("SSI Replication Data.csv")


##GROUP MEANS FOR TABLE 3 (BOTTOM HALF)
tapply(natl$MenBetterPol, natl[c("group")], mean, na.rm=TRUE)
tapply(natl$ControlWife, natl[c("group")], mean, na.rm=TRUE)

#create racial subsets of data
natl.whites = subset(natl, natl$Race == "White")
natl.blacks = subset(natl, natl$Race == "Black")

##SIGNIFICANCE TESTS FOR TABLE 3 (BOTTOM HALF)

t.test(natl.whites$MenBetterPol ~ natl.whites$group)
t.test(natl.whites$ControlWife ~ natl.whites$group)

t.test(natl.blacks$MenBetterPol ~ natl.blacks$group)
t.test(natl.blacks$ControlWife ~ natl.blacks$group)


##TABLE 6: Sexism and Vote Choice, National SSI Sample


natl.vote.W=lm(votetrump ~ MenBetterPol + ControlWife   + Woman + Age + Income + partisanship + trustgov +  ReligAttend + resentment  , data = natl.whites)
summary(natl.vote.W) 

natl.vote.B=lm(votetrump ~ MenBetterPol + ControlWife   + Woman + Age + Income + partisanship + trustgov +  ReligAttend + resentment  , data = natl.blacks)
summary(natl.vote.B) 

natl.vote.W.int=lm(votetrump ~ MenBetterPol*Woman + ControlWife + Age + Income + partisanship + trustgov +  ReligAttend + resentment  , data = natl.whites)
summary(natl.vote.W.int) 


#OUTPUT TABLE 6
stargazer(natl.vote.W, natl.vote.B, natl.vote.W.int, title="Sexism and Candidate Support, National Sample", align=TRUE, style="apsr", digits=2, column.labels=c("Whites", "Blacks", "Whites"), single.row = TRUE,  star.char=c("*", "**"), star.cutoffs=c(0.10, 0.05))


##FIGURE 2: Relative Effect Sizes from Regression Models of White Respondents (from each data set)

##From non-interaction models of White respondents in Tables 5 and 6

summary(vote.whites)
summary(natl.vote.W) 


##Exit Poll Data

eff.exit = c(0.210888 ,  0.119193, 0.165412, 0.183069, 0.220101, -0.143031, 0.10346,  -0.017325, 0.24692, 0.043185, 0.171462, -0.063944  ,  0.199467 , 0.002126 , -0.095916 , 0.106518, -0.041445 , -0.053928 , 0.051886, 0.025174)
labels_exit = c("Men better suited" ,  "Control wife" , "No special treatment for blacks" , "Illegal immigration" , "Perceived voter fraud" , "Trust in media" , "Regulation" , "Less government" , "Ideology" , "Religious attendance" , "Biblical literalism" , "Woman" , "Republican" , "Other registration" , "Foreign born" , "Age" , "Income" , "Black precinct" , "Latino precinct" , "Mixed precinct")
d.exit = data.frame(eff.exit, labels_exit)
levels(d.exit$labels_exit)

d.exit$labels_exit = factor(d.exit$labels_exit , levels(d.exit$labels_exit)[c(11 , 4, 13, 7, 15 , 19, 16, 10, 6, 17, 2, 20, 14, 18, 5, 1, 8, 3, 9, 12)])



#OUTPUT EXIT POLL PART OF FIGURE 2
plot.eff.exit = ggplot(d.exit, aes(factor(labels_exit), eff.exit, label=labels_exit)) + 
  geom_bar(stat="identity", fill=c("skyblue4"), width=0.5) +
  geom_hline(yintercept = 0) + 
  coord_flip() + 
  ylim(-.3 , .3) + 
  xlab(c("")) + 
  ylab(c("Expected Change in Trump Vote")) + 
  theme_bw() + 
  theme(axis.text = element_text(size = 12), axis.title.x = element_text(face="plain", colour="black", size=12), axis.title.y = element_text(face = "plain", colour = "black", size = 12))

##SSI Data

eff.natl = c(0.11406 ,  0.0508044, 0.0008833, 0.0826599, -0.0318923, 0.8812932, 0.0950034 , 0.0520869, 0.3272224)
labels_natl = c("Men better suited" ,  "Control wife" , "Woman" , "Age", "Income", "Partisanship", "Trust in government" , "Religious attendance" , "Racial resentment" )
d.natl = data.frame(eff.natl, labels_natl)
levels(d.natl$labels_natl)

d.natl$labels_natl = factor(d.natl$labels_natl , levels(d.natl$labels_natl)[c(4 , 2, 6, 8, 7, 9, 5, 1, 3)])


#OUTPUT SSI PART OF FIGURE 2
plot.eff.natl = ggplot(d.natl, aes(factor(labels_natl), eff.natl, label=labels_natl)) + 
  geom_bar(stat="identity", fill=c("skyblue4"), width=0.5) +
  geom_hline(yintercept = 0) + 
  coord_flip() + 
  ylim(-.1 , .9) + 
  xlab(c("")) + 
  ylab(c("Expected Change in Trump Vote")) + 
  theme_bw() + 
  theme(axis.text = element_text(size = 12), axis.title.x = element_text(face="plain", colour="black", size=12), axis.title.y = element_text(face = "plain", colour = "black", size = 12))


